package leetcode.lcp0029;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {
	public int orchestraLayout(int num, int xPos, int yPos) {
		long up = xPos, down = num - xPos - 1;
		long left = yPos, right = num - yPos - 1;
		long min = Math.min(Math.min(left, right), Math.min(up, down));
		long total = (long) num * num;
		long inner = num - (min << 1);
		long part1 = total - inner * inner;
		long part2 = 0;
		if (up == min) {
			part2 = left - min + 1;
		} else if (down == min) {
			part2 = (inner << 1) + right - min - 1;
		} else if (left == min) {
			part2 = inner * 3 + down - min - 2;
		} else {
			part2 = inner + up - min;
		}
		return (int) ((part1 + part2 + 8) % 9) + 1;
	}

	@Override
	public void debug4() {
		HelpDebug.compare(1, this.orchestraLayout(449572, 209397, 306801));
		HelpDebug.compare(9, this.orchestraLayout(6408, 1889, 3386));
		HelpDebug.compare(4, this.orchestraLayout(7466, 7084, 2520));
		HelpDebug.compare(3, this.orchestraLayout(3, 0, 2));
		HelpDebug.compare(5, this.orchestraLayout(4, 1, 2));
		HelpDebug.compare(3, this.orchestraLayout(3, 0, 2));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
